<template>
	<view class="wrap">
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
		<u-form :model="form" label-width="96px" class="form" ref="form">
		  <u-form-item label="Debug模式">
		    <div class="form-col-12">
          <u-switch v-model="form.isDebug"></u-switch>
		    </div>
		  </u-form-item>
		  <u-form-item label="接口地址" label-position="top">
		    <div class="form-col-12">
		      <u-input v-model="form.adminPath" type="text" :border="true" class="u-input" :clearable="false" placeholder="请输入接口主机地址" />
		    </div>
		  </u-form-item>
		  <u-form-item label="保存日志">
		    <div class="form-col-12">
          <u-switch v-model="form.isLog"></u-switch>
		    </div>
		  </u-form-item>
    </u-form>
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
    <u-cell-group class="cell-group">
      <u-cell-item title="查看日志" @click="checkLog" :arrow="true"></u-cell-item>
    </u-cell-group>
    <div class="footer-fixed">
      <u-button @click="handleSubmit" class="btn-submit" type="primary">保存</u-button>
    </div>
	</view>
</template>

<script>
import { dater } from '@daelui/dogjs/dist/components.js'
import fileManager from '@/components/libs/file-data-manager.js'
import storeManager from '@/components/libs/store-data-manager.js'

// 是否是APP环境
let isPlus = false
// #ifdef APP-PLUS
isPlus = true
// #endif
const dataManager = isPlus ? fileManager : storeManager

// 获取调试配置
const getDebugConfig = function () {
  return uni.getStorageSync('debug') || {}
}

let dataDefer = Promise.resolve()
// 写入日志
const saveLog = function (e) {
  dataDefer = dataDefer.then(() => {
    return queryLog().then((res) => {
      let data = res.data
      data = Array.isArray(data) ? data : []
      if (typeof e === 'string' || typeof e === 'number') {
        e = { message: e }
      }
      e = e || {}
      e.date = dater.format(Date.now())
      data.unshift(e)
      return dataManager.writeData(data, 'log')
    })
  })

}

// 查询日志
const queryLog = function () {
  return dataManager.readData('log')
}

// 清除日志
const clearLog = function () {
  return dataManager.clearData((res) => {}, 'log')
}

export default {
	data() {
		return {
			form: {
        isDebug: false,
        adminPath: '',
        isLog: true
      }
		}
	},

  onShow () {
    const value = getDebugConfig()
    if (value && Object.prototype.hasOwnProperty.call(value, 'isDebug')) {
      Object.assign(this.form, value)
    }
  },

	methods: {
		handleSubmit () {
			uni.setStorageSync('debug', this.form)
      this.$u.toast('保存成功，退出重新登录生效')
		},
    checkLog () {
      uni.navigateTo({
        url: '/pages/sys/workbench/log'
      })
    }
	}
}

export {
  getDebugConfig, saveLog, queryLog, clearLog
}
</script>
<style lang="scss">
@import '../home/index.scss';
page {
	background-color: #fff;
}
.wrap {
  /deep/.u-switch {
    top: 6px;
  }
}
.footer-fixed {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px;
  background: #fff;
  border-top: 1px #ebeef5 solid;
  z-index: 9;
}
.btn-submit {
  width: 100%;
}
</style>
